//
//  UserSettingProfileController.m
//  SmallElephant
//
//  Created by Faith on 2022/12/24.
//

#import "UserSettingProfileController.h"
#import "UserSettingProfileModifyController.h"
#import "UserSettingProfileQrcodeController.h"

@interface UserSettingProfileController ()

/// 用户头像控件
@property (nonatomic, strong) UIImageView *avatarView;

/// 个人资料视图
@property (nonatomic, strong) UIView *profileView;

@end

@implementation UserSettingProfileController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置标题
    [self setTitleText:@"个人资料"];
    
    // 个人头像
    [self.scrollViewContent addSubview:self.avatarView style:@"display: flex; flex-direction: column; margin-top: 30px; width: 100px; height: 100px; border-radius: 50px;  align-self: center;"];
    UIImageView *iconView = [UIImageView withImage:@"icon-camera@3x"];
    [self.scrollViewContent addSubview:iconView style:@"display: flex; flex-direction: column; margin-top: -30px; margin-left: 70px; width: 25px; height: 25px; align-self: center;"];
    
    // 添加个人资料
    [self.scrollViewContent addSubview:self.profileView style:@"display: flex; flex-direction: column; border-radius: 10px; background: #FFFFFF; margin-left: 10px; margin-right: 10px; margin-top: 25px; padding-top: 5px; padding-bottom: 5px;"];
    
    // 加载数据
    [self loadData];
}

/// 懒加载用户头像控件
- (UIImageView *)avatarView {
    if(!_avatarView) {
        _avatarView = [[UIImageView alloc] init];
    }
    return _avatarView;
}

/// 构建列表组件
- (UIView *)buildList:(NSArray*)itemList {
    UIView *contentView = [[UIView alloc] init];
    for(int i=0; i<itemList.count; i++) {
        NSDictionary *itemData = itemList[i];
        
        UIView *itemView = [[UIView alloc] init];
        itemView.customData = itemData;
        itemView.userInteractionEnabled = YES;
        [itemView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnItemTapEventHandler:)]];
        [contentView addSubview:itemView style:@"display: flex; flex-direction: row; justify-content: space-between; padding-left: 15px;  padding-right: 15px; padding-top:10px; padding-bottom: 10px; align-items: flex-start;"];
        
        // 左边视图
        UILabel *profileTitleView = [[UILabel alloc] init];
        profileTitleView.text = itemData[@"title"];
        [itemView addSubview:profileTitleView style:@"display: flex; width: 90px; font-size: 15px; color: #333333;"];
        
        // 右边视图
        UIView *profileRightView = [[UIView alloc] init];
        [itemView addSubview:profileRightView style:@"display: flex; flex: 1; flex-direction: row; justify-content: flex-end; align-items: center; margin-left: 10px;"];
        if(itemData[@"value"]) {
            // 项目值视图
            if([itemData[@"type"] isEqualToString:@"image"]) {
                UIImageView *profileValueView = [UIImageView withImage:itemData[@"value"]];
                [profileRightView addSubview:profileValueView style:@"display: flex; width: 24px; height: 24px;"];
            } else {
                UILabel *profileValueView = [[UILabel alloc] init];
                profileValueView.numberOfLines = 0;
                profileValueView.text = itemData[@"value"];
                [profileRightView addSubview:profileValueView style:@"display: flex; flex: 1; text-align: right; font-size: 15px; color: #888888;"];
                if([itemData[@"align"] isEqualToString:@"left"]) {
                    [profileValueView applyStyle:@"text-align: left;"];
                }
            }
        }
        // 箭头视图
        UIButton *profileArrowView = [UIButton buttonWithType:UIButtonTypeCustom];
        profileArrowView.hidden = !itemData[@"arrow"] ? NO : ![itemData[@"arrow"] boolValue];
        [profileArrowView setImage:[UIImage systemImageNamed:@"chevron.right"] forState:UIControlStateNormal];
        profileArrowView.tintColor = [UIColor colorWithHexString:@"#BCBCBC"];
        [profileRightView addSubview:profileArrowView style:@"width: 24px; height: 24px;"];
        
    }
    return contentView;
}

/// 懒加载个人资料
- (UIView *)profileView {
    if(!_profileView) {
        NSArray *profileItemData = @[
            @{
                @"title": @"账号", @"value": @"32132132@qq.com", @"arrow": @(NO)
            },
            @{
                @"title": @"昵称", @"value": @"无名指的约定"
            },
            @{
                @"title": @"ID", @"value": @"56205620000", @"arrow": @(NO)
            },
            @{
                @"title": @"二维码", @"value": @"icon-qrcode@3x", @"type": @"image"
            },
            @{
                @"title": @"个性签名", @"value": @"iOS 15 引入了额外的默认布局调整，但通过设置 sectionFooterTopPadding、tableFooterView 以及调整 contentInset 和 contentInsetAdjustmentBehavior 等属性，可以完全移除底部的多余空白。", @"align": @"left"
            }
        ];
        _profileView = [self buildList:profileItemData];
    }
    return _profileView;
}

/// 项目点击事件
- (void)btnItemTapEventHandler:(UITapGestureRecognizer*)recognizer {
    NSDictionary *itemData = recognizer.view.customData;
    if([itemData[@"title"] isEqualToString:@"昵称"]) {
        UserSettingProfileModifyController *controller = [[UserSettingProfileModifyController alloc] init];
        controller.type = @"1";
        [self pushViewController:controller];
    }
    if([itemData[@"title"] isEqualToString:@"个性签名"]) {
        UserSettingProfileModifyController *controller = [[UserSettingProfileModifyController alloc] init];
        controller.type = @"2";
        [self pushViewController:controller];
    }
    if([itemData[@"title"] isEqualToString:@"二维码"]) {
        UserSettingProfileQrcodeController *controller = [[UserSettingProfileQrcodeController alloc] init];
        [self pushViewController:controller];
    }
}

/// 加载数据
- (void)loadData {
    [self.avatarView setImageUrl:@"https://wx4.sinaimg.cn/orj360/007HbL0Agy1hw485pjninj32c0340u0x.jpg"];
}


@end
